//
//  BaseTextCell.m
//  xWeibo
//
//  Created by alvis mac on 11/8/15.
//  Copyright © 2015 alvis. All rights reserved.
//

#import "BaseTextCell.h"
#import "AyIconView.h"
#import "TQRichTextView.h"
#import "AyDefine.h"
#import "UIImageExtend.h"
#import "NSStringExtend.h"
#import "BaseTextModel.h"

@interface BaseTextCell()
{
    // 背景view
    UIImageView *_bgView;
    UIImageView *_selectedBgView;
    AyIconView *_icon; // 头像
    UILabel *_screenName; // 昵称
    TQRichTextView *_text; // 具体内容
    UILabel *_time; // 事件
    
    UIImageView *_mb; // 微博会员图标
}
@end


@implementation BaseTextCell

+ (instancetype) baseTextCellWithUITableView:(UITableView *) table{
    static NSString * CID=@"basecell";
    BaseTextCell *cell= [table dequeueReusableCellWithIdentifier:CID];
    if (cell==nil) {
        cell=[[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CID];
    }
    cell.myTableView=table;
    return cell;
}



- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 添加子控件
        [self addChildViews];
    }
    return self;
}

- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
    int rowsCount = [_myTableView numberOfRowsInSection:indexPath.section];
    NSString *name = nil;
    if (indexPath.row == rowsCount - 1) {
        name = @"statusdetail_comment_background_bottom.png";
    } else {
        name = @"statusdetail_comment_background_middle.png";
    }
    
    _bgView.image = [UIImage resizedImageWithName:name];
    _selectedBgView.image = [UIImage resizedImageWithName:[name fileNameAppendString:@"_highlighted"]];
}

#pragma mark 添加子控件
- (void)addChildViews
{
    // 1.头像
    _icon = [[AyIconView alloc] initWithFrame:CGRectMake(kCommentMargin, kCommentMargin, 0, 0)];
    //_icon.iconType = IconTypeSmall;
    [self.contentView addSubview:_icon];
    
    // 2.昵称
    _screenName = [[UILabel alloc] init];
    _screenName.font = kScreenNameFont;
    _screenName.textColor = kScreenNameColor;
    _screenName.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_screenName];
    
    // 3.时间
    _time = [[UILabel alloc] init];
    _time.textColor = kTimeColor;
    _time.font = kTimeFont;
    _time.backgroundColor = [UIColor clearColor];
    CGFloat timeX = 2 * kCommentMargin + _icon.frame.size.width;
    CGFloat timeHeight = kTimeFont.lineHeight;
    CGFloat timeY = self.contentView.frame.size.height - timeHeight - kCommentMargin;
    _time.frame = CGRectMake(timeX, timeY, 200, timeHeight);
    _time.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.contentView addSubview:_time];
    
    // 4.内容
    _text = [[TQRichTextView alloc] init];
    _text.textColor = kTextColor;
    _text.font = kTextFont;
    //    _text.numberOfLines = 0;
    _text.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_text];
    
    // 5.微博会员图标
    _mb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_membership.png"]];
    [self.contentView addSubview:_mb];
    
    
    // 设置背景view
    _bgView = [[UIImageView alloc] init];
    self.backgroundView = _bgView;
    _selectedBgView =[[UIImageView alloc] init];
    self.selectedBackgroundView = _selectedBgView;
}

#pragma mark 设置数据
- (void)setBaseTextFrame:(BaseTextFrame *)baseTextFrame
{
    _baseTextFrame = baseTextFrame;
    BaseTextModel *textModel = baseTextFrame.textModel;
    // 1.昵称
    _screenName.frame = baseTextFrame.screenNameFrame;
    _screenName.text = textModel.user.name;
    
    // 2.文字
    _text.frame = baseTextFrame.textFrame;
    _text.text = textModel.text;
    
    // 3.时间
    _time.text = textModel.time;
    
    // 4.头像
    [_icon setUser:textModel.user iconType:IWIconTypeSmall];
    
    // 5.微博会员
    if (textModel.user.mbtype != 0) {
        _mb.hidden = NO;
        _mb.frame = baseTextFrame.mbFrame;
        
        _screenName.textColor = kMBScreenNameColor;
    } else {
        _mb.hidden = YES;
        _screenName.textColor = kScreenNameColor;
    }
}



#pragma mark - 父类方法
#pragma mark 调节子控件的位置
- (void)setFrame:(CGRect)frame
{
    frame.origin.x = AyStatusTableBorder+2;
    frame.size.width -= 2 * (AyStatusTableBorder+2);
    [super setFrame:frame];
}

#pragma mark - 子控件状态控制
#pragma mark 取消所有子控件的高亮状态
- (void)unhighlightedSubviews:(UIView *)view
{
    for (UIButton *subview in view.subviews)
    {
        if ([subview respondsToSelector:@selector(setHighlighted:)])
        {
            subview.highlighted = NO;
            [self unhighlightedSubviews:subview];
        }
    }
}
#pragma mark 重写父类方法
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    if (selected) {
        [self unhighlightedSubviews:self.contentView];
    }
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    
    if (highlighted) {
        [self unhighlightedSubviews:self.contentView];
    }
}

@end
